package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.laiyima.zhongjiang.linghuilv.demo.R;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class NewAddtaxActivity extends SwipeBackActivity implements View.OnClickListener {
    private String TAG="NewAddtaxActivity";
    private int newaddCode;

    private LinearLayout mImageView;
    private Button mButton;
    private TextView mTextView;
    private EditText newAddtaxAmount,newAddtaxContent;

    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_addtax);
        mImageView= (LinearLayout) findViewById(R.id.new_addtax_back);
        mButton= (Button) findViewById(R.id.new_add_tax);
        mTextView= (TextView) findViewById(R.id.new_addtax_billingName);
        newAddtaxAmount= (EditText) findViewById(R.id.new_add_taxAmount);
        newAddtaxContent= (EditText) findViewById(R.id.new_add_taxContent);

        mImageView.setOnClickListener(this);
        mButton.setOnClickListener(this);
        setStatusBar();
        intView();

    }

    private void intView() {
        Intent intent = getIntent();
        String show_BillingName = intent.getStringExtra("show_BillingName");
        mTextView.setText(show_BillingName);
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.new_addtax_back:
                this.finish();
                break;
            case R.id.new_add_tax:
                postView();
            break;
        }

    }

    private void postView() {
        String new_add_taxAmount=newAddtaxAmount.getText().toString();
        String new_add_taxContent=newAddtaxContent.getText().toString();
        Intent intent = getIntent();
        String show_show_addType = intent.getStringExtra("show_show_addType");
        String show_show_addBname = intent.getStringExtra("show_show_addBname");
        String show_show_addAddress = intent.getStringExtra("show_show_addAddress");
        String show_show_addEm = intent.getStringExtra("show_show_addEm");
        String show_show_addpID = intent.getStringExtra("show_show_addpID");
        String show_show_addTaxNo = intent.getStringExtra("show_show_addTaxNo");
        String show_show_addAccount = intent.getStringExtra("show_show_addAccount");
        String show_show_addBank = intent.getStringExtra("show_show_addBank");
        String show_show_addTMoblie = intent.getStringExtra("show_show_addTMoblie");
        String show_show_addPName = intent.getStringExtra("show_show_addPName");
        String show_show_addPAddress = intent.getStringExtra("show_show_addPAddress");
        String show_show_addPMoblie = intent.getStringExtra("show_show_addPMoblie");

        if (new_add_taxAmount.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入发票金额", Toast.LENGTH_SHORT).show();
            return ;
        }else if(new_add_taxContent.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入发票内容", Toast.LENGTH_SHORT).show();
            return ;
        }
        else{
            SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
            String userID = sharedPreferences.getString("userID", "");
            Log.e(TAG, "userID:::" + userID);
            RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/hjs_api/Invoice/add_invoice");
            //需要带入的参数
             params.addBodyParameter("uid", userID);
             params.addBodyParameter("invoiceType", show_show_addType);
             params.addBodyParameter("billingName", show_show_addBname);
             params.addBodyParameter("taxAddress", show_show_addAddress);
             params.addBodyParameter("emailAddress", show_show_addEm);
             params.addBodyParameter("proxyId", show_show_addpID);
             params.addBodyParameter("taxNo", show_show_addTaxNo);
             params.addBodyParameter("taxAccount", show_show_addAccount);
             params.addBodyParameter("taxOpenBank", show_show_addBank);
             params.addBodyParameter("taxMobile", show_show_addTMoblie);
             params.addBodyParameter("taxAmount", new_add_taxAmount);
             params.addBodyParameter("taxContent", new_add_taxContent);
             params.addBodyParameter("postName", show_show_addPName);
             params.addBodyParameter("postAddress", show_show_addPAddress);
             params.addBodyParameter("postMobile", show_show_addPMoblie);

            // params.addBodyParameter("captcha", captcha);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {

                    Log.e(TAG, "回调成功");
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        newaddCode = jsonObject.optInt("code", 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (newaddCode == 200) {
                        Intent NewAddtaxintent=new Intent(getBaseContext().getApplicationContext(),Tax_Instead_Activity.class);
                        startActivity(NewAddtaxintent);
                        Toast.makeText(getBaseContext().getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
                    }
                    if (newaddCode == 201) {
                        // Toast.makeText(getBaseContext().getApplicationContext(), "实名认证失败", Toast.LENGTH_SHORT).show();
                        Log.e(TAG, "保存成功失败");
                    }
                    if (newaddCode == 500) {
                        Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                        Log.e(TAG, "系统异常");
                    } else {
                        //Toast.makeText(getBaseContext().getApplicationContext(), "信息核实不正确", Toast.LENGTH_SHORT).show();
                        Log.e(TAG, "信息核实不正确");
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Log.e(TAG, "实名认证失败");
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }

    }
}
